package com.smalls.newvideotwo.utils;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.SharedPreferences;

import com.smalls.newvideotwo.util.LogUtil;

import java.util.List;

public class AppUtils {
    public static final String KEY_USER_ENTER_STATE = "key_user_enter_state";
    public static final String SHAREDPREFERENCES_CONFIG_NAME = "config";
    public static final String SHAREDPREFERENCES_VK_ADDR_NAME = "ukingSharedPreferences_addr";
    public static final String SHAREDPREFERENCES_VK_NAME = "ukingSharedPreferences";
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "AppUtils":"AppUtils";
    public static boolean isAppOnForeground(Context context) {
        @SuppressLint("ServiceCast") ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
        String packageName = context.getApplicationContext().getPackageName();
        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(packageName) && appProcess.importance == 100) {
                return true;
            }
        }
        return false;
    }
    public static int DipToPixels(Context context, float dip) {
        return (int) ((dip * context.getResources().getDisplayMetrics().density) + 0.5f);
    }
    public static String getPackageName(Context context) {
        String packageName = context.getApplicationContext().getPackageName();
        if (packageName.equalsIgnoreCase("com.supernet.yuehuikan")) {
            return "com.swl.yueplayback";
        }
        if (packageName.equalsIgnoreCase("com.supernet.yizhouhuikan")) {
            return "com.swl.aplayback";
        }
        if (packageName.equalsIgnoreCase("com.supernet.huanxidianshi")) {
            return "com.swl.onelive";
        }
        if (packageName.equalsIgnoreCase("hottv.box.adultos")) {
            return "org.coolx.sexytribe";
        }
        return packageName;
    }

    public static SharedPreferences getVKSharedPreferences(Context context) {
        return context.getSharedPreferences(SHAREDPREFERENCES_VK_NAME, 0);
    }

    public static SharedPreferences getVKAddrSharedPreferences(Context context) {
        return context.getSharedPreferences(SHAREDPREFERENCES_VK_ADDR_NAME, 0);
    }

    public static SharedPreferences getConfigSharedPreferences(Context context) {
        return context.getSharedPreferences(SHAREDPREFERENCES_CONFIG_NAME, 0);
    }

    public static boolean isFirstEnterApp(Context context) {
        SharedPreferences sp = getConfigSharedPreferences(context);
        boolean result = sp.getBoolean(KEY_USER_ENTER_STATE, true);
        LogUtil.d(TAG, "isFirstIn=" + result);
        if (result) {
            sp.edit().putBoolean(KEY_USER_ENTER_STATE, false).commit();
            LogUtil.d(TAG, "put=" + sp.getBoolean(KEY_USER_ENTER_STATE, true));
        }
        return result;
    }
}
